package fr.afcepf.atod18.wr.fee;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import org.springframework.beans.factory.annotation.Autowired;

import fr.afcepf.atod18.ws.currencies.dao.FeePersistence;
import fr.afcepf.atod18.ws.currencies.dao.ICountryDAO;
import fr.afcepf.atod18.ws.currencies.dao.IFeeDAO;

@Path("/ChangeFee")
@Produces("application/json")
public class ServiceChangeFee implements IServiceChangeFee {

    @Autowired
    private IFeeDAO feeDao;
    @Autowired
    private ICountryDAO countryDao;

    @GET
    @Path("/getFeeForCountry/{CountryName}")
    public List<FeeDTO> getFeeForCountry( 
            @PathParam("CountryName") String paramCountryName) {
        List<FeePersistence> lc = feeDao.getFeeByCountryName(paramCountryName);
        List<FeeDTO> returnList = new ArrayList<FeeDTO>();
        for (FeePersistence c : lc) {
            System.out.println("dans la boucle "+c);
            returnList.add(new FeeDTO(c.getCountryBean().getCountryName(), c.getMinAmount(), c.getMaxAmount(), c.getRate()));
        }
        System.out.println("return from getFeeForCountry");
        return returnList;
    }

    @GET
    @Path("/getFeeForAmountAndCountry/{Amount}/{CountryName}")
    public double getFeeForAmountAndCountry( @PathParam("Amount") double amount,
            @PathParam("CountryName") String paramCountryName) {
        System.out.println("getFeeForAmountAndCountry Amount "+amount+"  CountryName"+paramCountryName);
        List<FeePersistence> lc = feeDao.getFeeByCountryName(paramCountryName);
        System.out.println("getFeeForAmountAndCountry getFeeByCountryName "+lc.size());
        for (FeePersistence c : lc) {
            System.out.println("getFeeForAmountAndCountry Check Min "+c.getMinAmount()+ " Max "+c.getMaxAmount());
            if ( amount >= (double)c.getMinAmount() && (double)amount <= c.getMaxAmount()) {
                return amount * c.getRate() / 100.0;
            }
        }
        System.out.println("getFeeForAmountAndCountry no match for min and max ");
        return -1;
    }


}
